#include "OverLayer.h"
#include "PlayLayer.h"
#include "StartLayer.h"
#include "AudioManage.h"

static const char* FONT_BMP = "fonts/helvetica-32.fnt";
static const char* CONST_PNG_OVER_MENU = "Over/over_menu.png";
static const char* CONST_PNG_RESTART_N = "Over/btn_restart_N.png";
static const char* CONST_PNG_RESTART_S = "Over/btn_restart_S.png";
static const char* CONST_PNG_HOME_N = "Over/btn_home_N.png";
static const char* CONST_PNG_HOME_S = "Over/btn_home_S.png";
static const char* CONST_PNG_SHARE_N = "Over/btn_share_N.png";
static const char* CONST_PNG_SHARE_S = "Over/btn_share_S.png";
static const char* CONST_PNG_STAR_BASE = "Over/star_base.png";
static const char* CONST_PNG_STAR = "Over/star.png";
static const char* CONST_PNG_LEVELS[6] = { "Over/level0.png",
											"Over/level1.png",
											"Over/level2.png",
											"Over/level3.png",
											"Over/level4.png",
											"Over/level5.png"
											};
static const float CONST_RATIO = 0.8f;
static const float CONST_TIME_MOVE = 0.3f;
static const float CONST_TIME_STAR = 0.2f;
static const int MAX_SCORE = 20;

static Sprite* overMenuSprite;
static Sprite* stars[5];
static int scoreFinal = 0;
static int starsCount = 0;
static Label* scoreLabel;
static Label* highestLabel;
static Sprite* levelSprite;

OverLayer::OverLayer()
{
	LayerColor* backcolor = LayerColor::create(Color4B(0, 0, 0, 200));
	this->addChild(backcolor);

	Size visibleSize = Director::getInstance()->getVisibleSize();

	overMenuSprite = Sprite::create(CONST_PNG_OVER_MENU);
	overMenuSprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height * 0.6f));
	overMenuSprite->setScale(CONST_RATIO);
	overMenuSprite->setVisible(false);
	this->addChild(overMenuSprite);

	float width = overMenuSprite->getContentSize().width;
	float height = overMenuSprite->getContentSize().height;

	for (int i = 0; i < 5; i++)
	{
		Sprite* starBase = Sprite::create(CONST_PNG_STAR_BASE);
		starBase->setPosition(Vec2(width / 6 * (i + 1), height * 0.75f));
		starBase->setScale(CONST_RATIO * 0.8f);
		overMenuSprite->addChild(starBase);

		stars[i] = Sprite::create(CONST_PNG_STAR);
		stars[i]->setPosition(Vec2(width / 6 * (i + 1), height * 0.75f));
		stars[i]->setScale(CONST_RATIO * 0.8f);
		stars[i]->setVisible(false);
		overMenuSprite->addChild(stars[i]);
	}
	
	MenuItemImage* homeBtn = MenuItemImage::create(CONST_PNG_HOME_N, CONST_PNG_HOME_S,
		CC_CALLBACK_0(OverLayer::homeCallBack, this));
	homeBtn->setPosition(Vec2(width * 0.25f, height * 0.2f));

	MenuItemImage* restartBtn = MenuItemImage::create(CONST_PNG_RESTART_N, CONST_PNG_RESTART_S,
		CC_CALLBACK_0(OverLayer::restartCallBack, this));
	restartBtn->setPosition(Vec2(width * 0.5f, height * 0.2f));

	MenuItemImage* shareBtn = MenuItemImage::create(CONST_PNG_SHARE_N, CONST_PNG_SHARE_S,
		CC_CALLBACK_0(OverLayer::shareCallBack, this));
	shareBtn->setPosition(Vec2(width * 0.75f, height * 0.2f));

	Menu* overMenu = Menu::create(homeBtn, restartBtn, shareBtn, NULL);
	overMenu->setPosition(Vec2::ZERO);

	overMenuSprite->addChild(overMenu);

	scoreLabel = Label::createWithBMFont(FONT_BMP, "Your Score:0");
	scoreLabel->setPosition(Vec2(width * 0.5f, height * 0.45f));
	scoreLabel->setScale(0.85f);
	scoreLabel->setVisible(false);
	overMenuSprite->addChild(scoreLabel);

	highestLabel = Label::createWithBMFont(FONT_BMP, "Highest Score:0");
	highestLabel->setPosition(Vec2(width * 0.5f, height * 0.35f));
	highestLabel->setScale(0.85f);
	highestLabel->setVisible(false);
	overMenuSprite->addChild(highestLabel);

	levelSprite = Sprite::create();
	levelSprite->setPosition(Vec2(width * 0.5f, height * 0.6f));
	levelSprite->setVisible(false);
	overMenuSprite->addChild(levelSprite);
}

OverLayer::~OverLayer()
{
}

OverLayer* OverLayer::thisLayer = nullptr;

void OverLayer::showLayer(int score)
{
	scoreFinal = score;
	starsCount = score / (MAX_SCORE / 5);

	if (starsCount > 5)
	{
		starsCount = 5;
	}
	
	if (nullptr == thisLayer)
	{
		thisLayer = new OverLayer();
		PlayLayer::getInstance()->addChild(thisLayer, 20);
		thisLayer->setVisible(false);
	}

	PlayLayer::getInstance()->stopGame();
	thisLayer->setVisible(true);
	thisLayer->showMenu();
}

void OverLayer::showMenu()
{
	auto listener1 = EventListenerTouchOneByOne::create();
	listener1->setSwallowTouches(true);
	listener1->onTouchBegan = CC_CALLBACK_0(OverLayer::callBack, this);
	m_listener = listener1;

	_eventDispatcher->addEventListenerWithSceneGraphPriority(m_listener, this);
	
	char s[20];
	sprintf(s, "Your Score : %d", scoreFinal);
	scoreLabel->setString(s);
	scoreLabel->setVisible(true);

	int highestScore = UserDefault::sharedUserDefault()->getIntegerForKey("Highest");
	if (scoreFinal > highestScore)
	{
		UserDefault::sharedUserDefault()->setIntegerForKey("Highest",scoreFinal);
		highestScore = scoreFinal;
	}

	sprintf(s, "Highest Score : %d", highestScore);
	highestLabel->setString(s);
	highestLabel->setVisible(true);

	levelSprite->setTexture(CONST_PNG_LEVELS[starsCount]);
	levelSprite->setVisible(true);

	overMenuSprite->runAction(Sequence::create(
		FadeOut::create(0.0f),
		MoveBy::create(0.0f, Vec2(0, 80.0f)),
		Show::create(),
		Spawn::create(
		MoveBy::create(CONST_TIME_MOVE, Vec2(0, -100.0f)),
		FadeIn::create(CONST_TIME_MOVE),
		NULL
		),
		MoveBy::create(CONST_TIME_MOVE, Vec2(0, 20.0f)),
		CallFunc::create(CC_CALLBACK_0(OverLayer::showStars, this, 0)),
		NULL
		));
}

void OverLayer::showStars(int index)
{
	if (starsCount == index)
	{
		return;
	}

	int nextIndex = index + 1;

	stars[index]->runAction(
		Sequence::create(
		Show::create(),
		ScaleBy::create(CONST_TIME_STAR, 1.5f),
		CallFunc::create(CC_CALLBACK_0(OverLayer::showStars, this, nextIndex)),
		CallFunc::create([]{	AudioManage::bellSound(); }),
		ScaleBy::create(CONST_TIME_STAR, 1 / 1.5f),		
		NULL
		));
}

void OverLayer::disposeLayer()
{
	if (thisLayer != nullptr)
	{
		//delete thisLayer;
		thisLayer = nullptr;
	}
}

bool OverLayer::callBack()
{
	return true;
}

void OverLayer::homeCallBack()
{
	AudioManage::buttonSound();

	_eventDispatcher->removeEventListener(m_listener);
	
	PlayLayer::disposeLayer();

	auto scene = StartLayer::createScene();
	Director::getInstance()->replaceScene(scene);
}

void OverLayer::restartCallBack()
{
	AudioManage::buttonSound();

	_eventDispatcher->removeEventListener(m_listener);

	PlayLayer::disposeLayer();

	auto scene = PlayLayer::createScene();
	Director::getInstance()->replaceScene(scene);
}

void OverLayer::shareCallBack()
{
	//_eventDispatcher->removeEventListener(m_listener);
	//thisLayer->setVisible(false);
}